<template>
  <div style="padding: 20px;">
    <el-select v-model="user" placeholder="选择牛马" >
    <el-option
      v-for="item in users"
      :key="item"
      :label="item"
      :value="item">
    </el-option>
  </el-select>
    <el-select v-model="year" placeholder="选择年份" >
    <el-option
      v-for="item in years"
      :key="item"
      :label="item"
      :value="item">
    </el-option>
  </el-select>
  <el-select v-model="yue" placeholder="选择月份" @change="change">
    <el-option
      v-for="item in yues"
      :key="item"
      :label="item"
      :value="item">
    </el-option>
  </el-select>
  <el-select collapse-tags v-model="ri" placeholder="选择日" multiple="">
    <el-option
      v-for="item in ris"
      :key="item"
      :label="item"
      :value="item">
    </el-option>
  </el-select>
  {{ ri }}
  <div>
    <div style="margin-top: 20px;">
    <el-button type="primary" @click="send">发送</el-button>
    <el-button type="primary" @click="send2">导出</el-button>
  </div>
  </div>
  </div>
  
</template>
<script setup>
import * as XLSX from 'xlsx';
import {addmeal,getmeal} from "~/api/xm"
import {
        ElMessage,
        ElMessageBox
    } from 'element-plus'
import { onMounted, ref } from 'vue';
const users=ref([
  '刘锦辉',
  '吕钧钊',
  '童辉',
  '韩守威',
  '赵奕鑫',
  '许大豪',
  '崔泷翰',
  '吕晓成',
  '刘博',
])
const years=ref([])
const user=ref(null)
const year=ref(null)
const  yues=ref([])
const  yue=ref([])
const ris=ref([])
const ri=ref([])
function change(e){
  let n=new Date(year.value.slice(0,year.value.length-1), yue.value.slice(0,yue.value.length-1), 0).getDate()
  ris.value=[]
  ri.value=[]
  for(let i=1;i<=n;i++){
    ris.value.push(year.value+yue.value+(i+'日'))
  }
}
function send2(){
  getmeal({
    year:year.value,
    month:yue.value,
  }).then(res=>{
    const data =[]
    res.data.forEach((v,i)=>{
data.push({
  '序号':i+1,
  '姓名':v.name,
  '加班日期':v.date,
  '加班事由':'平台开发',
  '餐费补助金额':30,
})
    })

      // 将数据转换为工作表（worksheet）
      const ws = XLSX.utils.json_to_sheet(data);

      // 创建一个工作簿（workbook）
      const wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

      // 导出文件，指定文件名
      XLSX.writeFile(wb, `加班费餐补申请表${yue.value}.xlsx`);
  })
 
}
function send(){
  let obj={
    name:user.value,
    year:year.value,
    month:yue.value,
    list:ri.value
  }
  if(!user.value){
    return    ElMessage({
                            type: 'warning',
                            message:'员工不能为空',
                        })
  }
  if(!ri.value.length){
    return    ElMessage({
                            type: 'warning',
                            message:'日期不能为空',
                        })
  }
  addmeal(obj).then(res=>{
    if(res.code==500){
      ElMessage({
                            type: 'warning',
                            message: res.msg,
                        })
    }else{
      ElMessage({
                            type: 'success',
                            message: '发送成功',
                        })
    }
   
  })
  console.log(obj);
}
onMounted(()=>{
  year.value=(new Date().getFullYear())+'年'
  yue.value=(new Date().getMonth()+1)+'月'
  for(let i=new Date().getFullYear();i<new Date().getFullYear()+3;i++){
    years.value.push(i+'年')
  }
  for(let i=1;i<=12;i++){
    yues.value.push(i+'月')
  }
  change()
})
// console.log(new Date(2025, 5, 0).getDate())
</script>
<style>

</style>
